/***************************************************************************
 * 版权所有：深圳市一非科技有限公司
 * 文件名称：HandBlueIdea.m
 * 内容摘要：手环蓝牙协议命令处理类
 * 编辑作者：彭于
 * 版本编号：1.0.1
 * 创建日期：2016年02月25日
 ***************************************************************************/
#import "HandBlueIdea.h"

@implementation HandBlueIdea

/***************************************************************************
 * 方法名称：getAllHand
 * 功能描述：回复的万能信息
 * 输入参数：writeNum 关键词
 * 返回数据：
 ***************************************************************************/
- (NSData *)getAllHand:(int)writeNum
{
    NSMutableDictionary *writeDic = [[NSMutableDictionary alloc] init];
    [writeDic setObject:@"224" forKey:@"byte0"];
    [writeDic setObject:[NSString stringWithFormat:@"%i",writeNum] forKey:@"byte1"];
    [writeDic setObject:@"0" forKey:@"byte2"];
    [writeDic setObject:@"0" forKey:@"byte3"];
    
    NSString *writeInString = [self get16Data:writeDic andLeng:writeDic.allKeys.count];
    NSData *writeData = [self getWriteData:writeInString andLeng:writeInString.length/2];
    return writeData;
}

/***************************************************************************
 * 方法名称：getUserInfoHand
 * 功能描述：获取写入手环的个人信息
 * 输入参数：writeNum 关键词    nowDic 最新的信息
 * 返回数据：
 ***************************************************************************/
- (NSData *)getUserInfoHand:(int)writeNum andNowInfo:(NSDictionary *)nowDic
{
    int sex = 1;
    int height = 175;
    int weight1 = 2;
    int weight2 = 188;
    int buJu = 60;
    int age = 18;
    int mubu1 = 00;
    int mubu2 = 17;
    int mubu3 = 70;
    int weight = 700;
    int mubu = 6000;
    weight1 = weight/256;
    weight2 = weight%256;
    mubu1 = mubu/65536;
    mubu2 = (mubu%65536)/256;
    mubu3 = mubu%256;
    
    NSMutableDictionary *locSetDic = [[NSMutableDictionary alloc] init];
    [locSetDic setObject:[NSString stringWithFormat:@"%i",weight1] forKey:@"byte2"];
    [locSetDic setObject:[NSString stringWithFormat:@"%i",weight2] forKey:@"byte3"];
    [locSetDic setObject:[NSString stringWithFormat:@"%i",age] forKey:@"byte4"];
    [locSetDic setObject:[NSString stringWithFormat:@"%i",height] forKey:@"byte5"];
    [locSetDic setObject:[NSString stringWithFormat:@"%i",buJu] forKey:@"byte6"];
    [locSetDic setObject:[NSString stringWithFormat:@"%i",sex] forKey:@"byte7"];
    [locSetDic setObject:[NSString stringWithFormat:@"%i",mubu1] forKey:@"byte8"];
    [locSetDic setObject:[NSString stringWithFormat:@"%i",mubu2] forKey:@"byte9"];
    [locSetDic setObject:[NSString stringWithFormat:@"%i",mubu3] forKey:@"byte10"];
    
    NSMutableDictionary *writeDic = [[NSMutableDictionary alloc] init];
    [writeDic setObject:@"224" forKey:@"byte0"];
    [writeDic setObject:[NSString stringWithFormat:@"%i",writeNum] forKey:@"byte1"];
    [writeDic setObject:@"0" forKey:@"byte2"];
    [writeDic setObject:@"0" forKey:@"byte3"];
    
    if ([self isDicEqual:locSetDic andcomData:nowDic])
    {
        NSString *writeInString = [self get16Data:writeDic andLeng:writeDic.allKeys.count];
        NSData *writeData = [self getWriteData:writeInString andLeng:writeInString.length/2];
        return writeData;
    }
    else
    {
        [locSetDic setObject:@"225" forKey:@"byte0"];
        [locSetDic setObject:[NSString stringWithFormat:@"%i",writeNum] forKey:@"byte1"];
        [locSetDic setObject:@"0" forKey:@"byte11"];
        
        NSString *writeInString = [self get16Data:locSetDic andLeng:locSetDic.allKeys.count];
        NSData *writeData = [self getWriteData:writeInString andLeng:writeInString.length/2];
        return writeData;
    }
}

/***************************************************************************
 * 方法名称：getTimeHand
 * 功能描述：获取写入手环的时间
 * 输入参数：writeNum 关键词    nowDic 最新的信息
 * 返回数据：
 ***************************************************************************/
- (NSData *)getTimeHand:(int)writeNum andNowInfo:(NSDictionary *)nowDic
{
    long long int handUtc = [[nowDic objectForKey:@"time"] longLongValue];
    NSTimeInterval juTime = [self getNowDateFromatAnDate:[NSDate date]];
    long long int myUtc = [[NSDate date] timeIntervalSince1970]+juTime;
    
    if (handUtc == myUtc)
    {
        NSMutableDictionary *writeDic = [[NSMutableDictionary alloc] init];
        [writeDic setObject:@"224" forKey:@"byte0"];
        [writeDic setObject:[NSString stringWithFormat:@"%i",writeNum] forKey:@"byte1"];
        [writeDic setObject:@"0" forKey:@"byte2"];
        [writeDic setObject:@"0" forKey:@"byte3"];
        
        NSString *writeInString = [self get16Data:writeDic andLeng:writeDic.allKeys.count];
        NSData *writeData = [self getWriteData:writeInString andLeng:writeInString.length/2];
        return writeData;
    }
    else
    {
        myUtc = myUtc;
        long long time4 = myUtc / (256*256*256);
        long long time3 = (myUtc % (256*256*256))/65536;
        long long time2 = (myUtc % 65536)/256;
        long long time1 = myUtc % 256;
        
        NSMutableDictionary *locSetDic = [[NSMutableDictionary alloc] init];
        [locSetDic setObject:@"226" forKey:@"byte0"];
        [locSetDic setObject:[NSString stringWithFormat:@"%i",writeNum] forKey:@"byte1"];
        [locSetDic setObject:[NSString stringWithFormat:@"%lli",time4] forKey:@"byte2"];
        [locSetDic setObject:[NSString stringWithFormat:@"%lli",time3] forKey:@"byte3"];
        [locSetDic setObject:[NSString stringWithFormat:@"%lli",time2] forKey:@"byte4"];
        [locSetDic setObject:[NSString stringWithFormat:@"%lli",time1] forKey:@"byte5"];
        [locSetDic setObject:@"0" forKey:@"byte6"];
        
        NSString *writeInString = [self get16Data:locSetDic andLeng:locSetDic.allKeys.count];
        NSData *writeData = [self getWriteData:writeInString andLeng:writeInString.length/2];
        return writeData;
    }

}

/***************************************************************************
 * 方法名称：getSetInfoHand
 * 功能描述：获取写入手环的设置信息
 * 输入参数：writeNum 关键词    nowDic 最新的信息
 * 返回数据：
 ***************************************************************************/
- (NSData *)getSetInfoHand:(int)writeNum andNowInfo:(NSDictionary *)nowDic
{
    NSString *whellDiameter = @"2.096";
    int whellNum = (int)([whellDiameter floatValue]*1000);
    int whell1 = whellNum / 256;
    int whell2 = whellNum % 256;
    
    NSMutableDictionary *locSetDic = [[NSMutableDictionary alloc] init];
    [locSetDic setObject:@"1" forKey:@"byte2"];
    [locSetDic setObject:@"22" forKey:@"byte3"];
    [locSetDic setObject:@"0" forKey:@"byte4"];
    [locSetDic setObject:@"8" forKey:@"byte5"];
    [locSetDic setObject:@"0" forKey:@"byte6"];
    [locSetDic setObject:@"9" forKey:@"byte7"];
    [locSetDic setObject:@"0" forKey:@"byte8"];
    [locSetDic setObject:@"10" forKey:@"byte9"];
    [locSetDic setObject:@"60" forKey:@"byte10"];
    [locSetDic setObject:[NSString stringWithFormat:@"%i",whell1] forKey:@"byte11"];
    [locSetDic setObject:[NSString stringWithFormat:@"%i",whell2] forKey:@"byte12"];
    
    NSMutableDictionary *writeDic = [[NSMutableDictionary alloc] init];
    [writeDic setObject:@"224" forKey:@"byte0"];
    [writeDic setObject:[NSString stringWithFormat:@"%i",writeNum] forKey:@"byte1"];
    [writeDic setObject:@"0" forKey:@"byte2"];
    [writeDic setObject:@"0" forKey:@"byte3"];
    
    if ([self isDicEqual:locSetDic andcomData:nowDic])
    {
        NSString *writeInString = [self get16Data:writeDic andLeng:writeDic.allKeys.count];
        NSData *writeData = [self getWriteData:writeInString andLeng:writeInString.length/2];
        return writeData;
    }
    else
    {
        [locSetDic setObject:@"228" forKey:@"byte0"];
        [locSetDic setObject:[NSString stringWithFormat:@"%i",writeNum] forKey:@"byte1"];
        [locSetDic setObject:@"0" forKey:@"byte13"];
        [locSetDic setObject:@"0" forKey:@"byte14"];
        [locSetDic setObject:@"0" forKey:@"byte15"];
        [locSetDic setObject:@"0" forKey:@"byte16"];
        [locSetDic setObject:@"0" forKey:@"byte17"];
        [locSetDic setObject:@"0" forKey:@"byte18"];
        [locSetDic setObject:@"0" forKey:@"byte19"];
        
        NSString *writeInString = [self get16Data:locSetDic andLeng:locSetDic.allKeys.count];
        NSData *writeData = [self getWriteData:writeInString andLeng:writeInString.length/2];
        return writeData;
    }
}

/***************************************************************************
 * 方法名称：getResultData
 * 功能描述：获取关键字
 * 输入参数：
 * 返回数据：
 ***************************************************************************/
- (int)getResultData:(NSString *)myStr andMark:(NSString *)mark
{
    int j=0;
    Byte bytes[20];
    for(int i=0;i<[myStr length];i++)
    {
        int int_ch;
        
        unichar hex_char1 = [myStr characterAtIndex:i];
        int int_ch1;
        if(hex_char1 >= '0' && hex_char1 <='9')
            int_ch1 = (hex_char1-48)*16;
        else if(hex_char1 >= 'A' && hex_char1 <='F')
            int_ch1 = (hex_char1-55)*16;
        else
            int_ch1 = (hex_char1-87)*16;
        i++;
        
        unichar hex_char2 = [myStr characterAtIndex:i];
        int int_ch2;
        if(hex_char2 >= '0' && hex_char2 <='9')
            int_ch2 = (hex_char2-48);
        else if(hex_char2 >= 'A' && hex_char2 <='F')
            int_ch2 = hex_char2-55;
        else
            int_ch2 = hex_char2-87;
        int_ch = int_ch1+int_ch2;
        bytes[j] = int_ch;
        j++;
    }
    
    int res = 0;
    if ([mark isEqualToString:@"0"])
    {
        int h0 = bytes[0]&0xF0;
        res = h0>>4;
    }
    else if ([mark isEqualToString:@"1"])
    {
        int h0 = bytes[0]&0xFF;
        res = h0;
    }
    
    return res;
}

/***************************************************************************
 * 方法名称：get16Data
 * 功能描述：获取拼接的字符
 * 输入参数：
 * 返回数据：
 ***************************************************************************/
- (NSString *)get16Data:(NSDictionary *)dataDic andLeng:(NSInteger)length
{
    NSMutableString *dataString = [[NSMutableString alloc] initWithString:@""];
    
    int allNum = 0;
    
    for (int i = 0; i < length-1; i++)
    {
        NSString *key = [NSString stringWithFormat:@"byte%i",i];
        
        int num = [[dataDic objectForKey:key] intValue];
        
        allNum = allNum + num;
        
        NSString *str16 = [self ToHex:num];
        
        if (str16.length == 1)
        {
            str16 = [NSString stringWithFormat:@"0%@",str16];
        }
        
        [dataString appendString:str16];
    }
    
    int endNum = allNum % 256;
    
    NSString *str16End = [self ToHex:endNum];
    
    if (str16End.length == 1)
    {
        str16End = [NSString stringWithFormat:@"0%@",str16End];
    }
    
    [dataString appendString:str16End];
    
    return dataString;
}

/***************************************************************************
 * 方法名称：getWriteData
 * 功能描述：获取写入命令的原数据
 * 输入参数：
 * 返回数据：
 ***************************************************************************/
- (NSData *)getWriteData:(NSString *)myStr andLeng:(NSInteger)length
{
    int j=0;
    Byte bytes[length];
    
    for(int i=0;i<[myStr length];i++)
    {
        int int_ch; //// 两位16进制数转化后的10进制数
        
        unichar hex_char1 = [myStr characterAtIndex:i]; ////两位16进制数中的第一位(高位*16)
        int int_ch1;
        if(hex_char1 >= '0' && hex_char1 <='9')
            int_ch1 = (hex_char1-48)*16; //// 0 的Ascll - 48
        else if(hex_char1 >= 'A' && hex_char1 <='F')
            int_ch1 = (hex_char1-55)*16; //// A 的Ascll - 65
        else
            int_ch1 = (hex_char1-87)*16; //// a 的Ascll - 97
        i++;
        
        unichar hex_char2 = [myStr characterAtIndex:i]; ///两位16进制数中的第二位(低位)
        int int_ch2;
        if(hex_char2 >= '0' && hex_char2 <='9')
            int_ch2 = (hex_char2-48); //// 0 的Ascll - 48
        else if(hex_char2 >= 'A' && hex_char2 <='F')
            int_ch2 = hex_char2-55; //// A 的Ascll - 65
        else
            int_ch2 = hex_char2-87; //// a 的Ascll - 97
        
        int_ch = int_ch1+int_ch2;
        //        NSLog(@"int_ch=%d",int_ch);
        bytes[j] = int_ch; ///将转化后的数放入Byte数组里
        j++;
    }
    
    NSData *myda = [[NSData alloc] initWithBytes:bytes length:length];
    
    return myda;
}

/***************************************************************************
 * 方法名称：getNowDateFromatAnDate
 * 功能描述：获取国际时间差值
 * 输入参数：
 * 返回数据：
 ***************************************************************************/
- (int)getNowDateFromatAnDate:(NSDate *)anyDate
{
    //设置源日期时区
    NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];//或GMT
    //设置转换后的目标日期时区
    NSTimeZone* destinationTimeZone = [NSTimeZone localTimeZone];
    //得到源日期与世界标准时间的偏移量
    NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:anyDate];
    //目标日期与本地时区的偏移量
    NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:anyDate];
    //得到时间偏移量的差值
    NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
    
    return interval;
}

/***************************************************************************
 * 方法名称：isDicEqual
 * 功能描述：判断两个字典是否一样（只有一层key）
 * 输入参数：
 * 返回数据：
 ***************************************************************************/
- (BOOL)isDicEqual:(id)dic1 andcomData:(id)dic2
{
    NSString *dic1Str = [NSString stringWithFormat:@"%@",dic1];
    NSString *dic2Str = [NSString stringWithFormat:@"%@",dic2];
    
    if (![dic1Str isEqualToString:dic2Str])
    {
        return NO;
    }
    
    return YES;
}

/***************************************************************************
 * 方法名称：ToHex
 * 功能描述：十进制转换成十六进制
 * 输入参数：
 * 返回数据：
 ***************************************************************************/
- (NSString *)ToHex:(int)tenNum
{
    NSString *nLetterValue;
    NSString *str =@"";
    int ttmpig;
    for (int i = 0; i < 9; i++)
    {
        ttmpig=tenNum%16;
        tenNum=tenNum/16;
        switch (ttmpig)
        {
            case 10:
                nLetterValue =@"a";break;
            case 11:
                nLetterValue =@"b";break;
            case 12:
                nLetterValue =@"c";break;
            case 13:
                nLetterValue =@"d";break;
            case 14:
                nLetterValue =@"e";break;
            case 15:
                nLetterValue =@"f";break;
            default:
                nLetterValue = [NSString stringWithFormat:@"%i",ttmpig];
                
        }
        str = [nLetterValue stringByAppendingString:str];
        if (tenNum == 0)
        {
            break;
        }
        
    }
    return str;
}

@end
